home *** CD-ROM | disk | FTP | other *** search
- Path: mudskipper.cac.psu.edu!user
- From: fcusack@tdx.org (frank.)
- Newsgroups: comp.lang.c
- Subject: Re: simple code, argc, argv, strcmp()
- Date: Fri, 02 Feb 1996 16:21:47 -0400
- Organization: Penn State University, Center for Academic Computing
- Message-ID: <fcusack-0202961621470001@mudskipper.cac.psu.edu>
- References: <11f7cc$17261a.3b3@daprez> <4etj7c$bma@news.iag.net>
- NNTP-Posting-Host: mudskipper.cac.psu.edu
-
- In article <4etj7c$bma@news.iag.net>, jatmon@iag.net (John R Buchan) wrote:
-
- > In article <11f7cc$17261a.3b3@daprez>, otisg@panther.middlebury.edu says...
- > >
- > >Hi, a C question...
- > >this one has been bothering ALL afternoon !
- > >
- > >This snippet of code is supposed to call appropriate function based on what
- > >command line arguments are supplied.
- > >
- > >usage: <program> -e [SourceFile] RemoteFile
- > > or
- > > <program> -d [InFile]
- > >
- > >if the person doesn't use this right the program is supposed to call Usage()
- > >which is not here, but it's in my code, and if the person calls the program
- > >the correct way one of the two actions should be taken (encode or decode).
- > >
- > >Problem - I am doing something wrong and I almost always end up calling
- > >Usage() even if I use the program correctly.
- > >
- > >#include <stdio.h>
- > >#include <stdlib.h>
- > >#include <string.h>
- > >
- > >int main (int argc, char **argv) {
- > >
- > > void Usage (void);
- > > void Encode (int, char **);
- > > void Decode (int, char **);
- > >
- > > if (!strcmp(argv[1],"-d") || !strcmp(argv[1],"-e")) {
- > > Usage();
- > > }
- > <snip>
- >
- > I assume you mean this to call Usage if argv[1] is not "-e" and not "-d"?
- > Change the || to &&.
-
- The && test would _never_ pass. argv[1] could never be both "-d" and "-e".
- The || is correct here.
-
- >Of course, if no argument was passed, who knows what
- > will happen here. You should always test argc first to be certain that
- > the argv element exists.
-
- argv[1] is always guaranteed to exist. It is simply NULL if there were no
- arguments.
- ~Frank
- -- I am Pentium of Borg. Division is futile. You will be approximated. --
- -- If you build it, they will come --> http://www.tdx.org/~fcusack/ --
- -- PGP key fingerprint: 01 C0 C0 B9 CC 78 67 0F 3F 64 80 65 8B 0F F9 EA --
-